<!-- Licensed under a BSD license. See license.html for license -->
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <title>WebGL - Multiple Objects - Manual</title>
    <link type="text/css" href="../resources/webgl-tutorials.css" rel="stylesheet" />
</head>

<body>
    <div id="info">
        <div class="description">
            Draw multiple objects manually
        </div>
        <a href="https://webglfundamentals.org/webgl/lessons/zh_cn/webgl-drawing-multiple-things.html"
            target="_blank">WebGL 绘制多个物体</a>
    </div>
    <canvas id="canvas"></canvas>
</body>
<!--
for most samples webgl-utils only provides shader compiling/linking and
canvas resizing because why clutter the examples with code that's the same in every sample.
See http://webglfundamentals.org/webgl/lessons/webgl-boilerplate.html
and http://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html
for webgl-utils, m3, m4, and webgl-lessons-ui.
-->
<script src="../resources/webgl-utils.js"></script>
<script src="../resources/m4.js"></script>
<script src="../resources/primitives.js"></script>
<!-- vertex shader -->
<script id="3d-vertex-shader" type="x-shader/x-vertex">
attribute vec4 a_position;
attribute vec4 a_color;

uniform mat4 u_matrix;

varying vec4 v_color;

void main() {
    // Multiply the position by the matrix.
    gl_Position = u_matrix * a_position;

    // Pass the color to the fragment shader.
    v_color = a_color;
}
</script>
<!-- fragment shader -->
<script id="3d-fragment-shader" type="x-shader/x-fragment">
precision mediump float;

// 从顶点着色器中传入的值
varying vec4 v_color;

uniform vec4 u_colorMult;

void main() {
    gl_FragColor = v_color * u_colorMult;
}
</script>
<script>
    "use strict";

    function main() {
        // Get A WebGL context
        /** @type {HTMLCanvasElement} */
        var canvas = document.getElementById("canvas");
        var gl = canvas.getContext("webgl");
        if (!gl) {
            return;
        }

        var createFlattenedVertices = function (gl, vertices) {
            return webglUtils.createBufferInfoFromArrays(
                gl,
                primitives.makeRandomVertexColors(
                    primitives.deindexVertices(vertices),
                    {
                        vertsPerColor: 6,
                        rand: function (ndx, channel) {
                            return channel < 3 ? ((128 + Math.random() * 128) | 0) : 255;
                        }
                    })
            );
        };

        var sphereBufferInfo = createFlattenedVertices(gl, primitives.createSphereVertices(10, 12, 6));
        var cubeBufferInfo = createFlattenedVertices(gl, primitives.createCubeVertices(20));
        var coneBufferInfo = createFlattenedVertices(gl, primitives.createTruncatedConeVertices(10, 0, 20, 12, 1, true, false));

        // setup GLSL program
        var programInfo = webglUtils.createProgramInfo(gl, ["3d-vertex-shader", "3d-fragment-shader"]);

        function degToRad(d) {
            return d * Math.PI / 180;
        }

        var cameraAngleRadians = degToRad(0);
        var fieldOfViewRadians = degToRad(60);
        var cameraHeight = 50;

        // 每个物体需要的全局变量
        var sphereUniforms = {
            u_colorMult: [0.5, 1, 0.5, 1],
            u_matrix: m4.identity(),
        };
        var cubeUniforms = {
            u_colorMult: [1, 0.5, 0.5, 1],
            u_matrix: m4.identity(),
        };
        var coneUniforms = {
            u_colorMult: [0.5, 0.5, 1, 1],
            u_matrix: m4.identity(),
        };

        // 每个物体的平移量
        var sphereTranslation = [0, 0, 0];
        var cubeTranslation = [-40, 0, 0];
        var coneTranslation = [40, 0, 0];

        function computeMatrix(viewProjectionMatrix, translation, xRotation, yRotation) {
            var matrix = m4.translate(viewProjectionMatrix,
                translation[0],
                translation[1],
                translation[2]);
            matrix = m4.xRotate(matrix, xRotation);
            return m4.yRotate(matrix, yRotation);
        }

        requestAnimationFrame(drawScene);

        // Draw the scene.
        function drawScene(time) {
            time *= 0.0005;

            webglUtils.resizeCanvasToDisplaySize(gl.canvas);

            // Tell WebGL how to convert from clip space to pixels
            gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);

            gl.enable(gl.CULL_FACE);
            gl.enable(gl.DEPTH_TEST);

            // Clear the canvas AND the depth buffer.
            gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

            // Compute the projection matrix
            var aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
            var projectionMatrix =
                m4.perspective(fieldOfViewRadians, aspect, 1, 2000);

            // Compute the camera's matrix using look at.
            var cameraPosition = [0, 0, 100];
            var target = [0, 0, 0];
            var up = [0, 1, 0];
            var cameraMatrix = m4.lookAt(cameraPosition, target, up);

            // Make a view matrix from the camera matrix.
            var viewMatrix = m4.inverse(cameraMatrix);

            var viewProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);

            var sphereXRotation = time;
            var sphereYRotation = time;
            var cubeXRotation = -time;
            var cubeYRotation = time;
            var coneXRotation = time;
            var coneYRotation = -time;

            // ------ 绘制球体 --------

            gl.useProgram(programInfo.program);

            // 设置所需的属性变量
            webglUtils.setBuffersAndAttributes(gl, programInfo, sphereBufferInfo);

            sphereUniforms.u_matrix = computeMatrix(
                viewProjectionMatrix,
                sphereTranslation,
                sphereXRotation,
                sphereYRotation);

            // 设置刚才计算出的全局变量
            webglUtils.setUniforms(programInfo, sphereUniforms);

            gl.drawArrays(gl.TRIANGLES, 0, sphereBufferInfo.numElements);

            // ------ 绘制立方体 --------

            // 设置所需的属性变量
            webglUtils.setBuffersAndAttributes(gl, programInfo, cubeBufferInfo);

            cubeUniforms.u_matrix = computeMatrix(
                viewProjectionMatrix,
                cubeTranslation,
                cubeXRotation,
                cubeYRotation);

            // 设置刚才计算出的全局变量
            webglUtils.setUniforms(programInfo, cubeUniforms);

            gl.drawArrays(gl.TRIANGLES, 0, cubeBufferInfo.numElements);

            // ------ 绘制椎体 --------

            // 设置所需的属性变量
            webglUtils.setBuffersAndAttributes(gl, programInfo, coneBufferInfo);

            coneUniforms.u_matrix = computeMatrix(
                viewProjectionMatrix,
                coneTranslation,
                coneXRotation,
                coneYRotation);

            // 设置刚才计算出的全局变量
            webglUtils.setUniforms(programInfo, coneUniforms);

            gl.drawArrays(gl.TRIANGLES, 0, coneBufferInfo.numElements);

            requestAnimationFrame(drawScene);
        }
    }

    main();
</script>

</html>